import json

# 读取刑法文本
with open("crime_law.txt", "r", encoding="utf-8") as f:
    lines = f.readlines()

# 转换为知识库JSON格式
knowledge_entries = []
entry_id = 1
current_entry = None

for line in lines:
    line = line.strip()
    if not line:  # 跳过空行
        continue

    if line.startswith("@"):
        # 如果已有正在处理的条目，先保存
        if current_entry:
            knowledge_entries.append({
                "id": entry_id,
                "text": current_entry
            })
            entry_id += 1

        # 开始新条目
        current_entry = line[1:].strip()
    elif current_entry is not None:
        # 将当前行添加到当前条目
        current_entry += " " + line

# 保存最后一个条目
if current_entry:
    knowledge_entries.append({
        "id": entry_id,
        "text": current_entry
    })

# 保存为knowledge.json
with open("knowledge.json", "w", encoding="utf-8") as f:
    json.dump(knowledge_entries, f, ensure_ascii=False, indent=2)

print(f"已创建{len(knowledge_entries)}条知识条目")
